package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


// 收货地址
@Service
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private UserMapper userMapper;
    //显示列表地址
    @Override
    public List<Address> findList() {
        // 1.构建条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId, UserHolder.get().getId()); // 用户人id
        //调用mapper
        List<Address> addressList = addressMapper.selectList(qw);
        //返回结果
        return addressList;
    }
    //新增列表
    @Override
    public void save(Address address) {
        // 1.补全信息
        address.setUserId(UserHolder.get().getId()); // 用户id
        address.setIsDefault(0); // 默认地址

        //2.调用mapper
        addressMapper.insert(address);
    }

    //设置默认值
    @Override
    public void setDefault(Long id) {
        //1.把当前用户所有地址设置为默认
        //1.1设置改写内容
        Address address = new Address();
        address.setIsDefault(0);
        //1.2设置用户id为条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId,UserHolder.get().getId());
        //1.3更新 -->调用mapper
        addressMapper.update(address,qw);

        //2.将当前收货地址设置为默认
        Address address1 = new Address();
        address1.setId(id); // 收货地址id
        address1.setIsDefault(1); // 默认
        // 调用mapper
        addressMapper.updateById(address1);
    }

    //结算
    @Override
    public Address findDefault() {
        // 1.构建 条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getIsDefault,1 ); // 默认值 = 1
        qw.eq(Address::getUserId,UserHolder.get().getId() ); // 用户id

        // 2.调用mapper查并返回
        return addressMapper.selectOne(qw);
    }

    //回显地址
    @Override
    public Address findById(Long id) {
        // 先获取这个地址
        Address address = addressMapper.selectById(id);
        /*//根据地址id查询用户
        //构建条件
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getId,id);
        //调用mapper
        User user = userMapper.selectOne(qw);
        //将用户信息你设置到地址中
        address.setUserId(id);*/

        return address;
    }

    //修改地址
    @Override
    public void update(Address address) {
        //1.修改地址的信息
        addressMapper.updateById(address);
        /*//根据地址的id删除原来的用户信息
        //构建条件
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getIdNumber,address.getUserId());
        //调用mapper
        //新增用户信息
        addressMapper.insert(address);*/
    }

    //删除
    @Override
    public void delete(Long ids) {
        addressMapper.deleteById(ids);
    }


}
